package com.tz.system.user;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Service;

import com.tz.platform.common.Constants;
import com.tz.platform.utils.MD5Util;
import com.tz.system.cache.utils.ParameterPub;
import com.tz.system.user.model.UserInfo;

@Service
public class UserUtils {

	/**
	 * 返回session用户信息
	 * @param request
	 * @return
	 */
	public static UserInfo getSessionUser(HttpServletRequest request) {
		return (UserInfo) request.getSession().getAttribute(Constants.SEESION_USER);
	}
	
	
	/**
	 * 根据用户id和密码查询是否存在此用户
	 * @param userId
	 * @param oldPasswd
	 * @return
	 */
	public static boolean checkUserPwdValid(String userId, String passwd){
		try {
			String strPwdEncoded = MD5Util.EncodeString(passwd);
			UserInfo user = ParameterPub.getLoginUser(userId);
			if (user !=null && strPwdEncoded.equals(user.getUserPassword())) {
				return true;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}
	
	/**
	 * 设置用户的缓存
	 * @param request
	 * @param userName
	 */
	public static void setSessionUser(HttpServletRequest request,String userName) {
		UserInfo userInfo = ParameterPub.getLoginUser(userName);
		HttpSession session = request.getSession();
		session.setAttribute(Constants.SEESION_USER, userInfo);
	}
	
	
	
	

}
